package modele;

public class Inhabitant {
	protected City city;
	protected Account account;
	protected String name;

	public Inhabitant(City c) {
		city = c;
		account = new Account();
	}

	/**
	 * Put the letter in the postbox of the sender's city.
	 * 
	 * @param l
	 *            the letter
	 */
	public void sendLetter(Letter<?> l) {
		if (getBalance() >= l.getCost()) {
			account.debit(l.getCost());
			System.out.println(name + " paid $" + l.getCost() + " for a "
					+ l.getType() + ". Balance is now $" + getBalance() + ".");
			getCity().sendLetter(l);
		} else {
			System.out.println(name + " tried to pay " + l.getCost()
					+ " for a " + l.getType() + " but couldn't afford it.");
		}
	}

	/**
	 * Get a letter and react to it.
	 * 
	 * @param l
	 *            the letter
	 */
	public void receiveLetter(Letter<?> l) {
		System.out.println(name + " received a letter from " + l.getSender());
		l.doAction();
	}

	public City getCity() {
		return city;
	}

	public int getBalance() {
		return account.getBalance();
	}

	public void debit(int amount) {
		account.debit(amount);
	}

	public void credit(int amount) {
		account.credit(amount);
	}

	public void setName(String n) {
		name = n;
	}

	public String toString() {
		return name;
	}
}
